package org.qloudgen.txn.dilithium.cluster.pages;

import java.util.List;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.time.Duration;
import org.qloudgen.txn.dilithium.cluster.beans.Cluster;
import org.qloudgen.txn.dilithium.cluster.beans.NodeInfo;
import org.qloudgen.txn.dilithium.cluster.common.CommonValues;
import org.qloudgen.txn.dilithium.cluster.database.ClusterImpl;
import org.qloudgen.txn.dilithium.cluster.monitor.Monitor;
import org.qloudgen.txn.dilithium.cluster.pages.common.BasePage;

import com.google.common.collect.Lists;


@SuppressWarnings("unchecked")
public class ShowDetailPage extends BasePage{

	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	private static final long serialVersionUID = 1L;
	
	private transient List<NodeInfo> nodes = Lists.newArrayList();
	
	@SuppressWarnings({ "rawtypes" })
	public ShowDetailPage(PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
		int clusterID = pageParameters.get("clusterID").toInteger();
		
		//get default conf from metadb
		Cluster cluster = ClusterImpl.getClusterInfo(clusterID);
				
		final Monitor monitor = new Monitor(cluster);
		monitor.setNodes(nodes);
		
		Form form = new Form("form");
		this.add(form);

		final WebMarkupContainer group=new WebMarkupContainer("group"); 
		AjaxSelfUpdatingTimerBehavior ajaxSUTBehavior = new AjaxSelfUpdatingTimerBehavior(Duration.seconds(5)){
			
			private static final long serialVersionUID = 1L;

			@Override
			protected  void	onPostProcessTarget(AjaxRequestTarget target)  {

				monitor.execMonitor();
				target.add(group);	
				
			}
		};
		group.add(ajaxSUTBehavior);
		form.add(group);
		
		group.setRenderBodyOnly(false);
		group.setOutputMarkupId(true);
		
		final PageableListView nodeListView =new PageableListView( "nodeRows", nodes, CommonValues.NODE_LIST_NUMBER) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				final NodeInfo nodeInfo = (NodeInfo) item.getModelObject();
				item.add( new Label( "nodeType", nodeInfo.getNodeType()));
				item.add( new Label( "nodeName", nodeInfo.getNodeName()));
				item.add( new Label( "nodeIp", nodeInfo.getNodeIp()));
				item.add( new Label( "nodePort", nodeInfo.getNodePort()));
				item.add( new Label( "nodeStatus", nodeInfo.getNodeStatus()));
				
				if (item.getIndex() % 2 == 1) {
					item.add(new AttributeModifier("class",new Model("even")));
					} else {
					item.add(new AttributeModifier("class",new Model("odd")));
					}
			}
		};
		nodeListView.setOutputMarkupId(true);
		group.add(nodeListView);
		
		final AjaxPagingNavigator nav = new AjaxPagingNavigator("navigator", nodeListView);
		nav.setOutputMarkupId(true);
		group.add(nav);
	
		AjaxLink refresh = new AjaxLink("refresh"){

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				monitor.execMonitor();
				target.add(group);
				
			}
			
		};
		form.add(refresh);		
		
		addHeader();

		addReturnPage(ClusterMainPage.class,pp);
		
		addFooter();
	}

}
